﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CoolERP_Entities;
using CoolERP_BLL;
using CoolERP_Common;
using System.Collections;

namespace CoolERP_Web.User
{
    public partial class XSDD_Edit : BasePage
    {
        private Guid Xid
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : Guid.Parse(this.GetUrlParam("id").ToString()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindFirst();
        }

        private void BindFirst()
        {
            BindDepartment(this.ddlDepartments, EnumDepartment.Marketing, "请选择");
            BindCPYSLX(ddlYSLX, "请选择");
            List<XSDDMXB> mxbList = new List<XSDDMXB>();
            if (Xid != Guid.Empty)
            {
                using (XSDDBLL xBLL = new XSDDBLL())
                {
                    liTitle.Text = "修改";
                    XSDD entity = xBLL.GetModel(t => t.ID == Xid);
                    mxbList = entity.XSDDMXB.ToList();
                    hidKHID.Value = entity.KHID.ToString();
                    txtKH.Text = entity.ZL_KH.JIANCHENG;
                    if (!string.IsNullOrEmpty(entity.CHFS) && this.ddlCHFS.Items.FindByValue(entity.CHFS) != null)
                        this.ddlCHFS.SelectedValue = entity.CHFS;
                    if (entity.BMID != null && this.ddlDepartments.Items.FindByValue(entity.BMID.ToString()) != null)
                    {
                        this.ddlDepartments.SelectedValue = entity.BMID.ToString();
                        BindStaff(this.ddlDepartments.SelectedValue, this.ddlSaler, "请选择");
                        if (entity.RYID != null && this.ddlSaler.Items.FindByValue(entity.RYID.ToString()) != null)
                        {
                            this.ddlSaler.SelectedValue = entity.RYID.ToString();
                            hidStaff.Value = entity.RYID.ToString();
                        }
                    }
                    this.txtDHSJ.Text = entity.DHSJ == null ? "" : entity.DHSJ.Value.ToString("yyyy-MM-dd HH:mm");
                    this.txtCHSJ.Text = entity.CHSJ == null ? "" : entity.CHSJ.Value.ToString("yyyy-MM-dd HH:mm");
                    mxbList = entity.XSDDMXB.ToList();
                }
            }
            if (mxbList.Count <= 0)
            {
                XSDDMXB mx = new XSDDMXB();
                mxbList.Add(mx);
                divTotalMoney.Attributes["style"] = "text-align:right; display:none; margin:5px 0px; padding-right:230px";
            }
            else
            {
                string ids = string.Empty;
                foreach (var id in mxbList.Select(t => t.CPID))
                    ids += "," + id.ToString();
                hidSelIds.Value = ids;
                spanMoney.Text = mxbList.Sum(t => t.SL * t.DJ).Value.ToString("F");
                divTotalMoney.Attributes["style"] = "text-align:right; margin:5px 0px; padding-right:230px";
            }
            
            #region=====动态添加行=====
            TableRow tr;
            TableCell tc;
            TextBox txt;
            Literal li;
            HiddenField hid;
            DropDownList ddl;
            for (int i = 0; i < mxbList.Count; i++)
            {
                XSDDMXB mx = mxbList[i];
                string index = i.ToString();
                tr = new TableRow();
                //序号
                tc = new TableCell();
                li = new Literal();
                li.Text = (i + 1).ToString();
                tc.Controls.Add(li);
                tr.Controls.Add(tc);
                //产品名称
                tc = new TableCell();
                hid = new HiddenField();
                hid.ID = "hidGid_" + index;
                if (mx.CPID != null)
                    hid.Value = mx.CPID.ToString();
                tc.Controls.Add(hid);
                txt = new TextBox();
                txt.ID = "txtMC_" + index;
                txt.Text = mx.CPMC;
                txt.Enabled = mx.CPID == null ? true : false;
                tc.Controls.Add(txt);
                tr.Controls.Add(tc);
                //产品规格
                tc = new TableCell();
                txt = new TextBox();
                txt.ID = "txtGG_" + index;
                txt.Text = mx.CPGG;
                txt.Enabled = mx.CPID == null ? true : false;
                tc.Controls.Add(txt);
                tr.Controls.Add(tc);
                #region====颜色====
                tc = new TableCell();
                if (string.IsNullOrEmpty(mx.CPYS))
                {
                    //颜色类型
                    ddl = new DropDownList();
                    ddl.ID = "ddlYSLX_" + index;
                    BindCPYSLX(ddl, "请选择");
                    ddl.Attributes["onchange"] = "selYS(this)";
                    tc.Controls.Add(ddl);
                }
                else
                {
                    string[] ysStr = mx.CPYS.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    //颜色类型
                    ddl = new DropDownList();
                    ddl.ID = "ddlYSLX_" + index;
                    if (mx.CPID == null)
                    {
                        BindCPYSLX(ddl, "请选择");
                        ddl.Attributes["onchange"] = "selYS(this)";
                        if (ddl.Items.FindByValue(ysStr[0]) != null)
                            ddl.SelectedValue = ysStr[0];
                    }
                    else
                        ddl.Items.Add(new ListItem(ysStr[0], ysStr[0]));
                    ddl.Enabled = mx.CPID == null ? true : false;
                    tc.Controls.Add(ddl);
                    //颜色
                    ddl = new DropDownList();
                    ddl.ID = "ddlYS_" + index;
                    if (mx.CPID == null)
                    {
                        BindCPYS(ddl, ddl.SelectedValue, "请选择");
                        if (ddl.Items.FindByValue(ysStr[1]) != null)
                            ddl.SelectedValue = ysStr[1];
                    }
                    else
                        ddl.Items.Add(new ListItem(ysStr[1], ysStr[1]));
                    ddl.Enabled = mx.CPID == null ? true : false;
                    tc.Controls.Add(ddl);
                }
                tr.Controls.Add(tc);
                #endregion
                //单价
                tc = new TableCell();
                txt = new TextBox();
                txt.ID = "txtPrice_" + index;
                txt.Text = mx.DJ == null ? "" : mx.DJ.Value.ToString("F");
                txt.Attributes["style"] = "width:60px; text-align:center";
                txt.Attributes["onchange"] = "setPrice(this)";
                tc.Controls.Add(txt);
                tr.Controls.Add(tc);
                //数量
                tc = new TableCell();
                txt = new TextBox();
                txt.ID = "txtCount_" + index;
                txt.Text = mx.SL == null ? "" : mx.SL.Value.ToString();
                txt.Attributes["style"] = "width:60px; text-align:center";
                txt.Attributes["onchange"] = "setCount(this)";
                tc.Controls.Add(txt);
                tr.Controls.Add(tc);
                //金额
                tc = new TableCell();
                if (mx.DJ != null && mx.SL != null)
                    tc.Text = (mx.SL * mx.DJ).Value.ToString("F");
                tr.Controls.Add(tc);
                //备注
                tc = new TableCell();
                txt = new TextBox();
                txt.ID = "txtBZ_" + index;
                txt.Text = mx.BZ;
                tc.Controls.Add(txt);
                tr.Controls.Add(tc);
                //删除
                tc = new TableCell();
                tc.Text = string.Format("<img src='../Images/Common/btn_del0.png' onclick='DelGoods(this)' style='cursor:pointer' />");
                tr.Controls.Add(tc);

                tbGoodsInfo.Controls.Add(tr);
            }
            #endregion
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            ZL_YWLSH ywItem = new ZL_YWLSH();
            bool isAdd = true;
            bool result = false;
            using (XSDDBLL xBLL = new XSDDBLL())
            {
                XSDD entity = new XSDD();
                if (Xid != Guid.Empty)
                    entity = xBLL.GetModel(t => t.ID == Xid);
                else
                {
                    entity.ID = Guid.NewGuid();
                    entity.DDZT = 0;
                    entity.BH = GenYWLSH.GenBH(EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.XSDD), out ywItem, out isAdd);
                }
                entity.KHID = Guid.Parse(hidKHID.Value);
                entity.BMID = Guid.Parse(this.ddlDepartments.SelectedValue);
                entity.RYID = Guid.Parse(hidStaff.Value);
                entity.CZRYID = this.currentUser.UserID;
                entity.DHSJ = DateTime.Parse(this.txtDHSJ.Text.Trim());
                entity.CHSJ = DateTime.Parse(this.txtCHSJ.Text.Trim());
                entity.CHFS = this.ddlCHFS.SelectedValue;
                //明细表
                List<XSDDMXB> delMXB = entity.XSDDMXB.ToList();//旧的待删明细表
                Dictionary<string, object> dcList = JSONHelper.JSONToObject<Dictionary<string, object>>(hidGoodsList.Value);
                ArrayList goodsItems = (ArrayList)dcList["goodsList"];
                foreach (var goods in goodsItems)
                {
                    Dictionary<string, object> dc = (Dictionary<string, object>)goods;
                    XSDDMXB item = new XSDDMXB();
                    item.ID = Guid.NewGuid();
                    if (dc.ContainsKey("id") && !string.IsNullOrEmpty(dc["id"].ToString()))
                        item.CPID = Guid.Parse(dc["id"].ToString());
                    item.CPMC = dc["name"].ToString();
                    item.CPGG = dc["gg"].ToString();
                    item.CPYS = dc["yslx"] + "," + dc["ys"];
                    item.SL = Convert.ToDouble(dc["count"]);
                    item.DJ = Convert.ToDouble(dc["price"]);
                    item.BZ = dc["bz"].ToString();
                    entity.XSDDMXB.Add(item);
                }
                if (Xid != Guid.Empty)
                    result = xBLL.Update<XSDDMXB>(entity, delMXB);
                else
                    result = xBLL.Insert(entity);
            }
            if (result)
            {
                btnSave.Visible = false;
                labResult.Text = "保存成功";
                using (ZL_YWLSHBLL zBLL = new ZL_YWLSHBLL())
                {
                    if (ywItem.ID != Guid.Empty)
                    {
                        if (isAdd)
                            zBLL.Insert(ywItem);
                        else
                            zBLL.Update(ywItem);
                    }
                }
            }
            else
                labResult.Text = "保存失败";
        }
    }
}